<?php

namespace app\api\controller;


use app\api\model\GroupPv;
use app\api\model\PvLog;
use app\api\model\Register;
use app\api\model\ShareCash;
use app\api\model\shop\Order;
use app\api\model\UsersGroup;
use app\api\model\UsersShareMoney;
use app\common\controller\Api;
use app\admin\model\usermanage\Users as UsersModel;
use think\Exception;

/**
 * 分销接口
 */
class Share extends Api
{

    protected $noNeedRight = '*';

    //赠送pv值
    public function giveAwayPv()
    {
        try {
            $realname = input('realname','');
            $account = input('account','');
            $pv = input('pv','');
            $order_id = input('order_id',0);

            $pv_log_model = new PvLog();

            $res = $pv_log_model->give($realname,$account,$pv,$order_id,$this->auth->id);
        } catch (\Exception $e) {

            $this->error($e->getMessage());
        }

        $this->success('',$res);
    }

    //我的团队
    public function getTeam()
    {
        $list = UsersModel::where(['parent_id'=>$this->auth->id])
            ->field('id,account,nickname,avatar_url,parent_id,time,is_distributor,realname')
            ->paginate(10, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            $list[$k]['time'] = date('Y-m-d',$v['time']);
            $list[$k]['is_distributor'] = $v['is_distributor']==1?'已激活':'未激活';
        }
        $this->success('',$list);
    }

    //pv值赠送记录
    public function getPvLog()
    {
        $list = PvLog::alias('p')
            ->join('users u', 'u.id=p.receive_user_id', 'LEFT')
            ->where(['p.user_id'=>$this->auth->id])
            ->where('p.type','<>',3)
            ->field('p.*,u.nickname')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }

    //余额明细
    public function sharePrice()
    {
        $list = UsersShareMoney::where(['user_id'=>$this->auth->id])
            ->where('money','>',0)
            ->paginate(10, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            if($v['source']==7){
                $order = Order::get($v['order_id']);
                $order_sn = $order?$order['order_sn']:'';

                $list[$k]['content'] = '店面激活奖励,订单编号:'.$order_sn;
            } elseif($v['source']==8){
                $order = Order::get($v['order_id']);
                $order_sn = $order?$order['order_sn']:'';
                $list[$k]['content'] = '店补,订单编号:'.$order_sn;

            } elseif($v['source']==9){
                $list[$k]['content'] = '团队业绩奖励';
            }
        }
        $this->success('',$list);
    }

    //积分明细
    public function integralDetail()
    {
        $status = input('status',0);
        $where = ['user_id'=>$this->auth->id];
        if ($status==1){
            $where['integral'] = ['>',0];
        } else {
            $where['integral'] = ['<',0];
        }
        $list = Register::where($where)
            ->paginate(10, false, ['query' => request()->get()]);
        $newList = [];
        /* @var Register[] $list */
        foreach ($list as $k => &$v) {
            $newItem = [];

            switch ($v['type']) {
                case 3:
                    $newItem['content'] = "平台调整";
                    $newItem['content_1'] = "平台调整积分";
                    break;
                case 4:
                    $order = Order::get($v['order_id']);
                    $order_sn = $order?$order['order_sn']:'';
                    $newItem['content'] = "商城订单，订单号：".$order_sn;
                    $newItem['content_1'] = "购物抵扣积分";
                    break;
                case 6:
                    $order = Order::get($v['order_id']);
                    $order_sn = $order?$order['order_sn']:'';
                    $newItem['content'] = "商城订单，订单号：".$order_sn;
                    $newItem['content_1'] = "积分退还";
                    break;
                case 7:
                    $order = Order::get($v['order_id']);
                    $order_sn = $order?$order['order_sn']:'';
                    $newItem['content'] = "商城订单，订单号：".$order_sn;
                    $newItem['content_1'] = "购物送积分";
                    break;
                case 17:
                    $newItem['content'] = "团队业绩奖励";
                    $newItem['content_1'] = "团队业绩奖励";
                    break;
                default:
                    if ($v['integral'] >= 0) {
                        $newItem['content'] = "积分增加";
                        $newItem['content_1'] = "赠送积分";
                    } else {
                        $newItem['content'] = "积分减少";
                        $newItem['content_1'] = "扣除积分";
                    }
                    break;
            }
            $newItem['integral'] = $v['integral'];
            $newList[] = $newItem;
        }
        $this->success('',$newList);
    }

    //提现信息
    public function info()
    {
        $info = ShareCash::where(['user_id'=>$this->auth->id])->order('createtime desc')->find();
        $this->success('',$info?$info:'');
    }

    //提现
    public function apply()
    {
        try {
            $cash = input('cash',0);
            $name = input('name','');
            $mobile = input('mobile','');
            $bank_name = input('bank_name','');
            $share_cash_model = new ShareCash();

            $res = $share_cash_model->apply($cash,$this->auth->id,$name,$mobile,$bank_name);
        }  catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('申请成功',$res);
    }

    //团队业绩
    public function teamPvLog()
    {
        $Agroup = UsersGroup::where(['user_id'=>$this->auth->id,'type'=>0])
            ->field('id,group_name')
            ->order('createtime asc')->select();
        $Agroup_pv = GroupPv::where(['first_group_id'=>$Agroup[0]['id']])
            ->field('id,first_group_id,new_group_id,first_pv,new_pv,status')
            ->order('createtime asc')->select();
        foreach ($Agroup_pv as $k=>$v){
            $exist = \app\admin\model\usermanage\Users::get(['group_id'=>$v['new_group_id']]);
            $A_first_group_name = UsersGroup::get($v['first_group_id']);
            $A_new_group_name = UsersGroup::get($v['new_group_id']);
            $Agroup_pv[$k]['first_group_name'] = $A_first_group_name ?  $A_first_group_name->group_name:'';
            $Agroup_pv[$k]['new_group_name'] = $A_new_group_name ?  $A_new_group_name->group_name:'';
            if (!$exist)unset($Agroup_pv[$k]);
        }
        $Bgroup = UsersGroup::where(['user_id'=>$this->auth->id,'type'=>1])
            ->field('id,group_name')
            ->order('createtime asc')->select();
        $Bgroup_pv = GroupPv::where(['first_group_id'=>$Bgroup[0]['id']])
            ->field('id,first_group_id,new_group_id,first_pv,new_pv,status')
            ->order('createtime asc')->select();
        foreach ($Bgroup_pv as $k=>$v){
            $exist = \app\admin\model\usermanage\Users::get(['group_id'=>$v['new_group_id']]);
            $B_first_group_name = UsersGroup::get($v['first_group_id']);
            $B_new_group_name = UsersGroup::get($v['new_group_id']);
            $Bgroup_pv[$k]['first_group_name'] = $B_first_group_name ? $B_first_group_name->group_name:'';
            $Bgroup_pv[$k]['new_group_name'] = $A_new_group_name ? $B_new_group_name->group_name:'';
            if (!$exist)unset($Bgroup_pv[$k]);
        }
        $data = [
            'pv'=>$this->auth->pv,
            'A'=>array_values($Agroup_pv),
            'B'=>array_values($Bgroup_pv),
        ];
        $this->success('',$data);
    }
}